import QtQuick 1.0
import Qt 4.7
import com.nokia.symbian 1.0
import "../js/Storage.js" as Storage

Window {
    id: window

    signal downloadSession
    signal downloadType
    signal downloadRoom
    signal downloadSponsor
    signal updateSession

    FontLoader {
        id: fontStyle
        name: "Helvetica"
    }

    PageStack {
        id: pageStack

        anchors.fill: parent
        toolBar: toolBar
    }

    StatusBar {
        id: statusBar

        anchors { top: parent.top; left: parent.left; right: parent.right }
    }

    ToolBar {
        id: toolBar

        anchors { bottom: parent.bottom; left: parent.left; right: parent.right }
    }

    FavoritePage {
        id: favorite

        tools: ToolBarLayout {
            ToolButton {
                iconSource: "../images/star_icon.png"

            }

            ToolButton {
                iconSource: "toolbar-list"
                onClicked: {
                    pageStack.push(sessionType);
                }
            }

            ToolButton {
                iconSource: "../images/calendar.svg"
                onClicked: {
                    sessionSchedule.downloadTime(1);
                    pageStack.push(sessionSchedule);
                }
            }

            ToolButton {
                iconSource: "../images/realtime_icon.png"
                onClicked: pageStack.push(realTimeSearch);
            }

            ToolButton {
                iconSource: "toolbar-menu"
                onClicked: mainMenu.open();
            }
        }
    }

    Menu {
        id: mainMenu

        content: MenuLayout {
            MenuItem {
                text: "Update Sessions"
                onClicked: updateSession();
            }
            MenuItem {
                text: "Sponsors"
                onClicked: pageStack.push(sponsor);
            }
            MenuItem {
                text: "About COSCUP"
                onClicked: pageStack.push(aboutOpen);
            }
            MenuItem {
                text: "Quit"
                onClicked: Qt.quit();
            }
        }
    }

    Dialog {
        id: errorDialog

        title {
            Text {
                anchors.left: parent.left
                anchors.leftMargin: 20
                anchors.verticalCenter: parent.verticalCenter

                text: "Network error"
                color: "white"

                font.pixelSize: 25
                font.family: fontStyle.name
            }
        }

        content {
            Text {
                anchors.left: parent.left
                anchors.leftMargin: 20
                anchors.verticalCenter: parent.verticalCenter

                text: "Cannot get data!"
                color: "white"

                font.pixelSize: 25
                font.family: fontStyle.name
            }
        }

        buttons {
            Button {
                width: 150

                anchors.centerIn: parent

                text: "OK"

                onClicked: errorDialog.close();
            }
        }
    }

    SessionTypePage {
        id: sessionType

        tools: ToolBarLayout {
            ToolButton {
                iconSource: "toolbar-back"
                onClicked: {
                    pageStack.pop();
                    favorite.downloadFav();
                }
            }

        }
    }

    SessionOpenPage {
        id: sessionOpen

        tools: ToolBarLayout {
            ToolButton {
                iconSource: "toolbar-back"
                onClicked: pageStack.pop();
            }

        }
    }

    SessionSchedulePage {
        id: sessionSchedule

        tools: ToolBarLayout {
            ToolButton {
                iconSource: "toolbar-back"
                onClicked: {
                    pageStack.pop();
                    favorite.downloadFav();
                }
            }

            ButtonRow {
                id: dayButton

                ToolButton {
                    id: day1
                    text: " 8/20 "

                    onClicked: {
                        sessionSchedule.downloadTime(1);
                        dayButton.checkedButton = day1
                    }
                }
                ToolButton {
                    id: day2
                    text: " 8/21 "

                    onClicked: {
                        sessionSchedule.downloadTime(2);
                        dayButton.checkedButton = day2
                    }
                }
            }

        }

    }

    SessionOpenDetailPage {
        id: sessionOpenDetail

        tools: ToolBarLayout {
            ToolButton {
                iconSource: "toolbar-back"
                onClicked: pageStack.pop();
            }
        }
    }

    RealTimePage {
        id: realTimeSearch

        tools: ToolBarLayout {
            ToolButton {
                iconSource: "toolbar-back"

                onClicked: {
                    realTimeSearch.reloadWeb("plurk");
                    pageStack.pop();
                }
            }

            ButtonRow {
                id: realTimeButton

                ToolButton {
                    id: plurk
                    text: " Plurk "

                    onClicked: {
                        realTimeSearch.reloadWeb("plurk");
                        dayButton.checkedButton = plurk
                    }
                }
                ToolButton {
                    id: twitter
                    text: " Twitter "

                    onClicked: {
                        realTimeSearch.reloadWeb("twitter");
                        dayButton.checkedButton = twitter
                    }
                }
            }
        }

    }


    SponsorPage {
        id: sponsor

        tools:  ToolBarLayout {
            ToolButton {
                iconSource: "toolbar-back"

                onClicked: {
                    pageStack.pop();
                }
            }
        }
    }

    SponsorOpenPage {
        id: sponsorOpen

        tools:  ToolBarLayout {
            ToolButton {
                iconSource: "toolbar-back"

                onClicked: {
                    pageStack.pop();
                }
            }
        }
    }

    AboutPage {
        id: aboutOpen

        tools:  ToolBarLayout {
            ToolButton {
                iconSource: "toolbar-back"

                onClicked: {
                    pageStack.pop();
                }
            }
        }
    }


    LodingIndicator {
        id: loadingData
    }

    ListModel {
        id: storageSessionModel
    }

    ListModel {
        id: storageTypeModel
    }

    ListModel {
        id: storageRoomModel
    }

    ListModel {
        id: storageSponsorModel
    }

    WorkerScript {
        id: fetchSessionData
        source: "../js/FetchSessionData.js"

        onMessage: {
            if (messageObject.result == "PROGRESS") {
                loadingData.open()
                loadingData.running = true
            } else if (messageObject.result == "SUCCESS") {
                loadingData.close()
                loadingData.running = false
                Storage.setSessionCache(storageSessionModel);
                sessionSchedule.downloadTime(1);
            } else {
                loadingData.close()
                loadingData.running = false
                errorDialog.open();
            }
        }
    }

    WorkerScript {
        id: fetchUpdateSessionData
        source: "../js/FetchSessionData.js"

        onMessage: {
            if (messageObject.result == "PROGRESS") {
                loadingData.open()
                loadingData.running = true
            } else if (messageObject.result == "SUCCESS") {
                loadingData.close()
                loadingData.running = false
                Storage.setSessionCache(storageSessionModel);
                sessionType.downloadType();
                sessionSchedule.downloadTime(1);
            } else {
                loadingData.close()
                loadingData.running = false
                errorDialog.open();
            }
        }
    }

    WorkerScript {
        id: fetchTypeData
        source: "../js/FetchTypeData.js"

        onMessage: {
            if (messageObject.result == "PROGRESS") {
                loadingData.open()
                loadingData.running = true
            } else if (messageObject.result == "SUCCESS") {
                loadingData.close()
                loadingData.running = false
                Storage.setTypeCache(storageTypeModel);
                sessionType.downloadType();
            } else {
                loadingData.close()
                loadingData.running = false
                errorDialog.open();
            }
        }
    }

    WorkerScript {
        id: fetchRoomData
        source: "../js/FetchRoomData.js"

        onMessage: {
            if (messageObject.result == "PROGRESS") {
                loadingData.open()
                loadingData.running = true
            } else if (messageObject.result == "SUCCESS") {
                loadingData.close()
                loadingData.running = false
                Storage.setRoomCache(storageRoomModel);
            } else {
                loadingData.close()
                loadingData.running = false
                errorDialog.open();
            }
        }
    }

    WorkerScript {
        id: fetchSponsorData
        source: "../js/FetchSponsorData.js"

        onMessage: {
            if (messageObject.result == "PROGRESS") {
                loadingData.open()
                loadingData.running = true
            } else if (messageObject.result == "SUCCESS") {
                loadingData.close()
                loadingData.running = false
                Storage.setSponsorCache(storageSponsorModel);
            } else {
                loadingData.close()
                loadingData.running = false
                errorDialog.open();
            }
        }
    }

    onDownloadSession: {
        var msg = {"model": storageSessionModel};
        fetchSessionData.sendMessage(msg);
    }

    onUpdateSession: {
        var msg = {"model": storageSessionModel};
        fetchUpdateSessionData.sendMessage(msg);
    }

    onDownloadType: {
        var msg = {"model": storageTypeModel};
        fetchTypeData.sendMessage(msg);
    }

    onDownloadRoom: {
        var msg = {"model": storageRoomModel};
        fetchRoomData.sendMessage(msg);
    }

    onDownloadSponsor: {
        var msg = {"model": storageSponsorModel};
        fetchSponsorData.sendMessage(msg);
    }



    Component.onCompleted: {
        Storage.init();
        if (Storage.getSessionCache(storageSessionModel) == "Unknown") {
            downloadSession();
        }

        if (Storage.getTypeCache(storageTypeModel) == "Unknown") {
            downloadType();
        }

        if (Storage.getTypeCache(storageTypeModel) == "Unknown") {
            downloadRoom();
        }

        if (Storage.getSponsorCache(storageSponsorModel) == "Unknown") {
            downloadSponsor();
        }

        sessionType.downloadType();
        sessionSchedule.downloadTime(1);
        favorite.downloadFav();

        pageStack.push(favorite);
    }

}

